package javaFeaures;

public class Draw {
    private int height; // 属性：高度

    // 构造函数，设置高度
    public Draw(int height) {
        this.height = height;
    }

    // 绘制三角形
    public void drawTriangle() {
        if (height < 3) {
            System.out.println("高度至少为3");
            return;
        }
        for (int i = 1; i <= height; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

    // 绘制矩形
    public void drawRectangle() {
        for (int i = 1; i <= height; i++) {
            for (int j = 1; j <= height; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

    // 绘制正方形
    public void drawSquare() {
        drawRectangle(); // 正方形可以看作是特殊的矩形，边长相等
    }

    // 绘制平行四边形
    public void drawParallelogram() {
        for (int i = 1; i <= height; i++) {
            for (int j = 1; j <= height - i; j++) {
                System.out.print("  ");
            }
            for (int j = 1; j <= height; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }


    // 主函数，用于创建Draw对象并绘制图形
    public static void main(String[] args) {
        Draw draw = new Draw(5); // 创建Draw对象，高度为5

        System.out.println("绘制三角形:");
        draw.drawTriangle();
        System.out.println();

        System.out.println("绘制矩形:");
        draw.drawRectangle();
        System.out.println();

        System.out.println("绘制正方形:");
        draw.drawSquare();
        System.out.println();

        System.out.println("绘制平行四边形:");
        draw.drawParallelogram();
        System.out.println();
    }
}
